package com.zeropoint.demo.controller;


import com.alibaba.fastjson.JSONObject;

import com.github.pagehelper.PageInfo;
import com.zeropoint.demo.api.EvaluateService;
import com.zeropoint.demo.pojo.BasicQuestionnaireInfo;
import com.zeropoint.demo.pojo.FormTotal;
import com.zeropoint.demo.pojo.FormTotalPor;
import com.zeropoint.demo.pojo.ResultEntity;
import com.zeropoint.demo.service.FormTotalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@CrossOrigin
public class Controller {

    @Autowired
    private FormTotalService formTotalService;

    @Autowired
    private HttpServletResponse response;

    /**
     * 评估
     * @return
     */
   @RequestMapping(value = "/evaluate", produces = {"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8"})
   public String evaluate(@RequestBody String json){

       FormTotal formTotal = JSONObject.parseObject(json,FormTotal.class);
       // 返回结果
       EvaluateService evaluateService = new EvaluateService(formTotal);
       String evaluate = evaluateService.evaluate();
       //保存结果
       formTotal.getQuestionnaire_base_info().homestyle = evaluate;
       //封装
       FormTotalPor formTotalPor = new FormTotalPor().setFormTotal(formTotal).setW(evaluateService.getApi().getW());
       formTotalService.addFormTotalPor(formTotalPor);
       System.out.println(evaluate);
       return evaluate;
   }

   @GetMapping("/get/basicInfo")
   public ResultEntity<PageInfo<BasicQuestionnaireInfo>> getBasicInfoList(@RequestParam("currentPage")Integer currentPage,
                                                                          @RequestParam("pageSize")Integer pageSize){
       try{
           PageInfo<BasicQuestionnaireInfo> pageInfo = formTotalService.queryBasicInfo(currentPage, pageSize);
           return ResultEntity.successWithData(pageInfo);
       }catch (Exception e) {
           return ResultEntity.falseWithoutData(e.getMessage());
       }
   }

   @DeleteMapping("/basicInfo")
   public ResultEntity<Boolean> deleteBasicInfoList(@RequestParam("questionnaire_id") Integer questionnaire_id){

       try{
           formTotalService.deleteBasicInfo(questionnaire_id);
           return ResultEntity.successWithData(true);
       }catch (Exception e){
           return ResultEntity.falseWithoutData(e.getMessage(), "404");
       }
   }

    /**
     * 导出多 Sheet 页实现
     */
    @PostMapping("/exportManySheet")
    public void exportManySheet(@RequestBody Map<String, Object> data) {


        try{
            List<Integer> ids = (ArrayList)data.get("ids");
            int[] id = new int[ids.size()];
            int i = 0;
            for (Integer integer : ids) {
                id[i ++] = integer;
            }

            formTotalService.exportSheets(response,id);

        }catch (Exception e){
            e.printStackTrace();
        }


    }


}
